<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/6/30
 * Time: 下午2:33
 */

namespace app\admin\controller;


use app\src\base\helper\ConfigHelper;
use app\src\powersystem\enum\AdminRoleEnum;
use app\src\powersystem\logic\AuthGroupAccessLogic;
use app\src\system\logic\ConfigLogic;
use app\src\withdrawDeposit\action\WithdrawDepositAction;
use app\src\withdrawDeposit\logic\WithdrawDepositLogic;
use think\Exception;

class WithdrawDeposit extends Admin
{

    /**
     * 提现申请列表
     */
    public function applyList()
    {

        if(IS_AJAX){

            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            $p = $this->_param('p', 1);
            $size = $this->_param('size', 15);
            $page = ['curpage' => $p, 'size' => $size];
            $apply_status = $this->_param('apply_status', 0);
            if(!in_array($apply_status,[0, 1, 2])) $this->error('error apply_status');

            $result = (new WithdrawDepositLogic)->applyList($page, $apply_status);

            if($result['status']){
                $this->success('获取提现申请列表成功', null, $result['info']);
            }else{
                $this->error('获取提现申请列表失败', null, $result['info']);
            }
        }

    }

    /**
     * 提现申请结果列表
     */
    public function queryList()
    {

        if(IS_AJAX){

            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            $p = $this->_param('p', 1);
            $size = $this->_param('size', 15);
            $page = ['curpage' => $p, 'size' => $size];
            $apply_status = ['in', [1, 2]];

            $result = (new WithdrawDepositLogic)->applyList($page, $apply_status);

            if($result['status']){
                $this->success('获取提现申请结果列表成功', null, $result['info']);
            }else{
                $this->error('获取提现申请结果列表失败', null, $result['info']);
            }
        }

    }

    /**
     * 通过提现申请
     */
    public function passApply()
    {
        if(IS_AJAX){

            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            $apply_id = $this->_param('apply_id');
            if(empty($apply_id)) $this->error('提现申请id缺失');

            $result = (new WithdrawDepositLogic)->passApply($apply_id);

            if($result['status']){
                $this->success('操作成功', null, $result['info']);
            }else{
                $this->error('操作失败', null, $result['info']);
            }
        }
    }

    /**
     * 否决提现申请
     */
    public function rejectApply()
    {
        if(IS_AJAX){

            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            $apply_id = $this->_param('apply_id');
            if(empty($apply_id)) $this->error('提现申请id缺失');

            $result = (new WithdrawDepositLogic)->rejectApply($apply_id);

            if($result['status']){
                $this->success('操作成功', null, $result['info']);
            }else{
                $this->error('操作失败', null, $result['info']);
            }
        }
    }

    public function exportApplyListExcel()
    {
        if(!$this->isBackAdmin()){
            $this->error('没有权限查看此内容');
        }

        $page = ['curpage' => 1, 'size' => 99999];
        $apply_status = 0;
        $result = (new WithdrawDepositLogic)->applyList($page, $apply_status);
        if(!$result['status']){
            return 'error';
        }

        $data = [];
        $head = ['#', ['申请时间',20], ['提现人',20], '提现金额(元)', ['开户行',35], ['银行卡号',30], '姓名', ['联系方式',20]];
        $i = 1;
        foreach ($result['info']['list'] as $val){
            $apply_info = $val['apply_info'];
            if(empty($apply_info)) continue;
            $apply_info = json_decode($apply_info, true);
            $data[] = [
                $i,
                date('Y-m-d H:i', $val['create_time']),
                $val['apply_name'],
                intval($val['score']) / 100,
                $apply_info['bank_card_name'],
                ' '.$apply_info['bank_card_no'],
                $apply_info['name'],
                ' '.$apply_info['mobile'],
            ];

            $i++;
        }

        (new \excel\Excel())->createExcel($head, $data, '提现申请'.date('Y-m-d H:i:s'));

    }

    /**
     * 获取提现金额限制
     */
    public function getWithdrawLimit()
    {
        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            $c = ConfigHelper::getValue('WITHDRAW_LIMIT');

            $this->success('获取提现金额限制成功', null, $c);
        }



    }

    /**
     * 设置提现金额限制
     */
    public function saveWithdrawLimit()
    {
        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }
            $limit_config = $this->_param('limit_config');
            if(empty($limit_config)) $this->error('配置参数缺失');
            $limit_config = json_decode($limit_config);

            $ccc = '';
            foreach ($limit_config as $key => $value) {
                $ccc .= $key .':' .$value ."\n";
            }
            $config = [
                'WITHDRAW_LIMIT' => $ccc
            ];

            $result = (new ConfigLogic())->set($config);
            if($result['status']){
                //清除缓存
                \think\Cache::set("config_" . session_id() . '_' . UID ,null);
                cache('global_config', null);
                $this->success('操作成功', null, $result['info']);
            }else{
                $this->success('操作失败', null, $result['info']);
            }

        }
    }

}